<?php
namespace Home\Controller;

/**
 * UserController
 * 用户信息
 */
class UserController extends CommonController {
    /**
     * 用户列表
     * @return
     */
    public function index() {
        $result = $this->getPagination('User',['is_del'=>0]);
        foreach($result['data'] as $key => $val){
            $data = $result['data'][$key];
            if($data['novel_ids'] != ""){
                $novel_arr = array_filter(explode(',',$data['novel_ids']));
                $novel = [];
                foreach($novel_arr as $v){
                    $novel[] = M('novel')->where(['id'=>$v])->getField('title');
                }

                $result['data'][$key]['novel_ids'] = implode(' ，',$novel);
            }else{
                $result['data'][$key]['novel_ids'] = "书架暂无小说";
            }

        }

        $this->assign('user', $result['data']);
        $this->assign('rows_count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }


    /**
     * 用户阅读记录列表
     * @return
     */
    public function read_record_index() {
        $result = $this->getPagination('Readrecord',['is_del'=>0]);
        foreach($result['data'] as $key => $val){
            $result['data'][$key]['novel_title'] = M('novel')->where(['id'=>$val['novel_id']])->getField('title');
            $result['data'][$key]['chapter_title'] = M('chapter')->where(['id'=>$val['chapter_id']])->getField('title');
            $result['data'][$key]['percent'] = 100*$val['percent'].'%';
        }

        $this->assign('read_record', $result['data']);
        $this->assign('rows_count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }

    /**
     * 删除阅读记录
     */
    public function read_record_del(){
        $Read_record_Model = M('readrecord');
        $id = I('post.id');
        if(!$id){
            $this->ajaxReturn(['code'=>-1,'msg'=>'参数错误，请重试']);
        }

        $result = $Read_record_Model->where(['id'=>$id])->setField('is_del',1);
        if($result){
            $this->ajaxReturn(['code'=>0,'msg'=>'删除成功!']);
        }else{
            $this->ajaxReturn(['code'=>-1,'msg'=>'删除失败，请重试!']);
        }
    }


    /**
     * 用户反馈记录列表
     * @return
     */
    public function feed_back_index() {
        $result = $this->getPagination('Feedback',['is_del'=>0]);
        foreach($result['data'] as $key=>$val){
            $result['data'][$key]['content'] = M('feedback_config')->where(['id'=>$val['config_id']])->getField('problem');
        }

        $this->assign('feedback', $result['data']);
        $this->assign('rows_count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }


    /**
     * 删除反馈记录
     */
    public function feed_back_del(){
        $Feed_back_Model = M('feedback');
        $id = I('post.id');
        if(!$id){
            $this->ajaxReturn(['code'=>-1,'msg'=>'参数错误，请重试']);
        }

        $result = $Feed_back_Model->where(['id'=>$id])->setField('is_del',1);
        if($result){
            $this->ajaxReturn(['code'=>0,'msg'=>'删除成功!']);
        }else{
            $this->ajaxReturn(['code'=>-1,'msg'=>'删除失败，请重试!']);
        }
    }

    /**
     * 用户反馈记录列表
     * @return
     */
    public function feedback_config_index() {
        $result = $this->getPagination('FeedbackConfig',['is_del'=>0]);

        $this->assign('feedback_config', $result['data']);
        $this->assign('rows_count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }

    /**
     * 添加反馈配置
     */
    public function feedback_config_add(){
        $this->display();
    }

    /**
     * 添加反馈配置操作
     */
    public function feedback_config_insert(){
        $feedback_config_Model = M('feedback_config');
        $data = I('post.data');
        if (!isset($data)) {
            return $this->errorReturn('无效的操作！');
        }

        if($data['problem'] == '' || !$data['problem']){
            return $this->errorReturn('问题描述不能为空！');
        }

        $result = $feedback_config_Model->add($data);
        if (!$result) {
            return $this->errorReturn('添加失败，稍后请重试！');
        }

        return $this->successReturn('添加成功！', U('User/feedback_config_index'));
    }


    /**
     * 编辑反馈配置页面
     * @return
     */
    public function feedback_config_edit() {
        if (!isset($_GET['id'])) {
            return $this->error('参数错误，稍后请重试！');
        }

        //榜单详情
        $feedback_config = M('feedback_config')->getById($_GET['id']);

        $this->assign('feedback_config', $feedback_config);

        $this->assign('id',$_GET['id']);
        $this->display();
    }

    /**
     * 更新反馈配置
     * @return
     */
    public function feedback_config_update() {
        $feedback_config_Model = M('feedback_config');
        $data = I('post.data');
        if(!isset($data)){
            return $this->errorReturn('无效的操作！');
        }

        if($data['problem'] == '' || !$data['problem']){
            return $this->errorReturn('问题描述不能为空！');
        }

        $result = $feedback_config_Model->where(['id'=>$data['id']])->save($data);
        if (!$result) {
            return $this->errorReturn('操作失败，请重试！');
        }

        return $this->successReturn('更新信息成功！', U('User/feedback_config_index'));
    }

    /**
     * 删除反馈配置
     */
    public function feedback_config_del(){
        $Feedback_config_Model = M('feedback_config');
        $id = I('post.id');
        if(!$id){
            $this->ajaxReturn(['code'=>-1,'msg'=>'参数错误，请重试']);
        }

        $result = $Feedback_config_Model->where(['id'=>$id])->setField('is_del',1);
        if($result){
            $this->ajaxReturn(['code'=>0,'msg'=>'删除成功!']);
        }else{
            $this->ajaxReturn(['code'=>-1,'msg'=>'删除失败，请重试!']);
        }
    }
}
